package com.iplant.ams.controller.ams.fxt;

import com.iplant.ams.service.AMSService;
import com.iplant.ams.service.po.ams.asset.AMSAssetInfo;
import com.iplant.ams.service.po.ams.fxt.DMSMeasureLedger;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceAPI;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/DMSMeasureLedger")
public class DMSMeasureLedgerController extends BasicController<DMSMeasureLedger> {


    PoService<AMSAssetInfo> AssetInfoService;


    @Autowired
    public AMSService mAMSService;

    public DMSMeasureLedgerController() {
        super(DMSMeasureLedger.class);
        AssetInfoService=ORMUtils.GetPoService(AMSAssetInfo.class);
    }


    /**
     * 新增或更新
     */
    @TraceAPI(value = "新增或更新")
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            DMSMeasureLedger wDMSMeasureLedger = CloneTool.Clone(wParam.get("data"), DMSMeasureLedger.class);

            if (wDMSMeasureLedger == null || wDMSMeasureLedger.StandardModelID <= 0
                    || wDMSMeasureLedger.MeasureToolID <= 0) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            AMSAssetInfo wAMSAssetInfo ;
            if(wDMSMeasureLedger.AssetInfoID>0){
                ServiceResult<AMSAssetInfo>  wAssetInfoServiceResult=AssetInfoService.SelectPO(wLoginUser, wDMSMeasureLedger.AssetInfoID, "");
                wAMSAssetInfo=wAssetInfoServiceResult.Result;
                if (StringUtils.isNotEmpty(wAssetInfoServiceResult.getFaultCode())) {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR, wAssetInfoServiceResult.getFaultCode());
                    return wResult;
                }
                if(wAMSAssetInfo==null||wAMSAssetInfo.ID<=0){
                    wAMSAssetInfo = new AMSAssetInfo(wDMSMeasureLedger);
                }else{
                    wAMSAssetInfo.SetLedger(wDMSMeasureLedger);
                }
            }else{
                wAMSAssetInfo = new AMSAssetInfo(wDMSMeasureLedger);
            }
            wAMSAssetInfo.setUserInfo(wLoginUser);
            ServiceResult<Integer> wServiceResult = AssetInfoService.UpdatePo(wLoginUser,
                    wAMSAssetInfo);

            if (StringUtils.isNotEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
                return wResult;
            }

            wDMSMeasureLedger.setUserInfo(wLoginUser);
            wDMSMeasureLedger.AssetInfoID = wAMSAssetInfo.ID;
            wDMSMeasureLedger.Code = wAMSAssetInfo.Code;
            wServiceResult = mPoService.UpdatePo(wLoginUser, wDMSMeasureLedger);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wDMSMeasureLedger);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    /**
     * 新增或更新
     */
    @TraceAPI("批量新增或更新")
    @PostMapping("/UpdateList")
    public Object UpdateList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // ServiceResult<List<AMSAssetStandardModel>> wServiceResult = mPoService.UpdatePoList(wLoginUser, wParam);
            //成功后插入设备型号
            //默认使用设备类型第一个激活的
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            List<DMSMeasureLedger> wList = CloneTool.CloneArray(wParam.get("data"), DMSMeasureLedger.class);

            ServiceResult<Integer> wServiceResult = new ServiceResult<>();
            AMSAssetInfo wAMSAssetInfo;
            ServiceResult<AMSAssetInfo> wAssetInfoServiceResult;
            for (DMSMeasureLedger wDMSMeasureLedger : wList) {

                if (wDMSMeasureLedger == null || wDMSMeasureLedger.StandardModelID <= 0
                        || wDMSMeasureLedger.MeasureToolID <= 0) {
                    continue;
                }

                if(wDMSMeasureLedger.AssetInfoID>0){
                    wAssetInfoServiceResult=AssetInfoService.SelectPO(wLoginUser, wDMSMeasureLedger.AssetInfoID, "");
                    wAMSAssetInfo=wAssetInfoServiceResult.Result;
                    if (StringUtils.isNotEmpty(wAssetInfoServiceResult.getFaultCode())) {
                        wServiceResult.FaultCode+=wAssetInfoServiceResult.getFaultCode();
                        break;
                    }
                    if(wAMSAssetInfo==null||wAMSAssetInfo.ID<=0){
                        wAMSAssetInfo = new AMSAssetInfo(wDMSMeasureLedger);
                    }else{
                        wAMSAssetInfo.SetLedger(wDMSMeasureLedger);
                    }
                }else{
                    wAMSAssetInfo = new AMSAssetInfo(wDMSMeasureLedger);
                }
                wAMSAssetInfo.setUserInfo(wLoginUser);
                wServiceResult = AssetInfoService.UpdatePo(wLoginUser, wAMSAssetInfo);

                if (StringUtils.isNotEmpty(wServiceResult.getFaultCode())) {
                    break;
                }

                wDMSMeasureLedger.setUserInfo(wLoginUser);
                wDMSMeasureLedger.AssetInfoID = wAMSAssetInfo.ID;

                wDMSMeasureLedger.Code = wAMSAssetInfo.Code;
                wServiceResult = mPoService.UpdatePo(wLoginUser, wDMSMeasureLedger);
                if (StringUtils.isNotEmpty(wServiceResult.getFaultCode())) {
                    break;
                }
            }
            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wList, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }



}
